<?php

/* Data Entry
 *
 * Takes user input from JavaScript, checks user authentication, and adds data to database
 */

require("./dbconnect.inc.php");

// Grab data
extract ($_POST);

// Process
mysql_connect($host,$dbuser,$dbpassword);
@mysql_select_db($database) or die( "Unable to select database");

// CHECK IF TITLE EXISTS!!!!!!!!
// need code

$titlecheck = sprintf("SELECT * FROM data");
$titleresult=mysql_query($titlecheck);
$numtitles=mysql_numrows($titleresult);

$j=0;
while($j < $numtitles){
$dbtitle=mysql_result($titleresult,$j,"title");
if($title == $dbtitle){
	echo "That title is already in use. - Data not stored!";
	exit();
	}
$j++;
}

// Enter data
$query = sprintf("INSERT INTO data (title, data) VALUES ('%s','%s')", $title, $data);
		
if(mysql_query($query)){
	echo "<p><span style=\"color:blue;\">Data entered.</span></p>";
	}
else{
	echo"<p><span style=\"color:red;\">Failed to enter data:</span></p>";
	echo "Title: ".$title;
	echo "<br>";
	echo "Data: ".$data;
	}

mysql_close();
		


?>